# # -*- coding: utf-8 -*-
# #
# # Author:: Jonny
# # Date:: 2020/8/18
# '''
# todo 根据JSONPath豁免？ 根据JSONSchema豁免
#
# '''
#
# # “继承”json5中的方法
# from json5.lib import load, loads, dump, dumps
# from json5.version import VERSION
#
# import json5 as json5
#
# from handlers.common.lib.log_ import logger
#
#
# def trans_json_file_to_dict(file_path):
#     with open(file_path, 'r', encoding='utf8')as fp:
#         json_data = json5.load(fp)
#     return json_data
#
#
# def trans_dict_to_json_file(d1, file_path):
#     logger.info("file_path:%s" % file_path)
#     # with open(os.path.join(project_path, file_path), 'w', encoding='utf8')as fp:
#     with open(file_path, 'w', encoding='utf8')as fp:
#         fp.write(json5.dumps(d1, ensure_ascii=False))
#     fp.close()
#
#
# def _is_json_str(_josn_str):
#     try:
#         json5.loads(_josn_str)
#         return True
#     except:
#         return False
#
#
# def _is_iterable(_data):
#     # 非空数组、非空对象、json_str
#     if isinstance(_data, list) and len(_data) > 0:
#         return True
#     if isinstance(_data, dict) and len(_data) > 0:
#         return True
#     # if is_json_str(param):
#     #     return True
#
#
# def _get_value(_data, key_or_index):
#     try:
#         return _data[key_or_index]
#     except:
#         return
#
#
# def diff_json(j1, j2, p='$', res=[]):
#     '''
#     输出一个list
#     list中的没一个元素包含JSONPath-path、j1对应JSONPath的内容-v1、j2对应JSONPath的内容-v2
#     '''
#     # 传入load之后的json，可能是dict-object或者list/array
#     j1_type = type(j1)
#     j2_type = type(j2)
#     if j1_type != j2_type:
#         logger.info('参数类型不一致')
#     else:
#         if j1_type == dict:
#             _d = dict(j1, **j2)
#             for k in _d:
#                 j1_v = _get_value(j1, k)
#                 j2_v = _get_value(j2, k)
#                 if _is_iterable(j1_v) and _is_iterable(j2_v):
#                     p = p + '.' + str(k)
#                     diff_json(j1_v, j2_v, p, res)
#                 elif j1_v != j2_v:
#                     p_final = p + '.' + str(k)
#                     res.append({'path': p_final, 'v1': j1_v, 'v2': j2_v})
#         elif j1_type == list:
#             l1 = len(j1)
#             l2 = len(j2)
#             if l1 > l2:
#                 l = l1
#             else:
#                 l = l2
#             for i in range(l):
#                 l1_v = _get_value(j1, i)
#                 l2_v = _get_value(j2, i)
#                 if _is_iterable(l1_v) and _is_iterable(l2_v):
#                     p = p + '[' + str(i) + ']'
#                     diff_json(l1_v, l2_v, p, res)
#                 elif l1_v != l2_v:
#                     p_final = p + '[' + str(i) + ']'
#                     res.append({'path': p_final, 'v1': l1_v, 'v2': l2_v})
#     return res
#
#
# def diff_json_completely():
#     # 当元素为json_str时继续
#     pass
#
#
# if __name__ == "__main__":
#     # test_diff_json()
#     json_str1 = ""
#     json_str2 = ""
#
#     d1 = {"aaa": "1111",
#           "ccc": {
#               "ddd": "333",
#               'fff': {'ggg': '777'}
#           }}
#
#     d2 = {"aaa": "111",
#           "ccc": {
#               "ddd": "333",
#               'fff': {'ggg': '7777'}
#
#           },
#           "eee": "555"
#           }
#
#     d3 = [1, 2, 3]
#     d4 = [1]
#
#     d5 = {"aaa": "111"}
#     d6 = {"aaa": "222"}
#
#
#     def test_diff_json():
#         # res = diff_json(d5, d6)
#         # print(res)
#
#         res = diff_json(d3, d4)
#
#         print(res)
#
#
#     # run tests
#     test_diff_json()
